import * as TypeORM from "typeorm";
import Model from "./common";

declare var syzoj, ErrorMessage: any;

import User from "./user";
import Problem from "./problem";

@TypeORM.Entity()
export default class List extends Model {
  static cache = true;

  @TypeORM.PrimaryGeneratedColumn()
  id: number;

  @TypeORM.Column({ nullable: true, type: "varchar", length: 80 })
  title: string;

  @TypeORM.Index()
  @TypeORM.Column({ nullable: true, type: "integer" })
  owner: number;

  @TypeORM.Column({ nullable: true, type: "text" })
  introduction: string;

  @TypeORM.Column({ nullable: true, type: "text" })
  problems: string;

  @TypeORM.Column({ nullable: true, type: "text" })
  admins: string;

  holder?: User;

  async loadRelationships() {
    this.holder = await User.findById(this.owner);
  }

  async isSupervisior(user) {
    return user && (user.is_admin || this.owner === user.id || this.admins.split('|').includes(user.id.toString()));
  }

  async getProblems() {
    if (!this.problems) return [];
    return this.problems.split('|').map(x => parseInt(x));
  }
}
